export default{
	makeRow(v = 0) {
		const array = new Array(9);
		array.fill(v);
		return array;
	},
	makeMatrix(v = 0) {
		return Array.from({
			length: 9
		}, () => this.makeRow(v))
	},
	shuffle(array) {
		const length = array.length;
		for(let i = 0; i < array.length - 1; i++) {
			const j = i + Math.floor(Math.random() * (length - i));
			[array[i], array[j]] = [array[j], array[i]];
		}
		return array;
	},
	checkFillable(matrix,n,rowIndex,colIndex){
		const row = matrix[rowIndex];
		const col = this.makeRow().map((v,i)=>matrix[i][colIndex]);
		//获取宫数据
		
		const {boxIndex} = this.boxToolkit.converToBoxIndex(rowIndex,colIndex);
		const box = this.boxToolkit.getBoxCells(matrix,boxIndex)
		for(let i=0;i<9;i++){
			if(row[i]===n||col[i]==n||box[i]==n){
				return false;
			}
		}
		return true;
	},
	boxToolkit:{
		getBoxCells(matrix,boxIndex){
			const stratRowIndex = Math.floor(boxIndex/3)*3
			const startColIndex = boxIndex%3*3;
			const result = []
			for(let i=0;i<9;i++){
				const rowIndex = stratRowIndex+ Math.floor(i/3);
				const colIndex = startColIndex + i%3;
				result.push(matrix[rowIndex][colIndex])
			}
			return result;
		},
		converToBoxIndex(rowIndex,colIndex){
			return {
				boxIndex:Math.floor(rowIndex/3)*3 + Math.floor(colIndex/3),
				cellIndex:rowIndex%3*3+colIndex%3
			}
		},
		converFromBoxIndex(boxIndex,cellIndex){
			return {
				rowIndex :Math.floor(boxIndex/3)*3 + Math.floor(cellIndex/3),
				colIndex:boxIndex%3*3+cellIndex%3
			}
		}
		
	}
};